#include<vector>
using namespace std;
class Solution {
public:
    int maxScoreSightseeingPair(vector<int>& A) {
        int res = 0;
        int index = A.size() - 1;
        for (int i = A.size() - 2; i >= 0; --i) {
            int tmp = A[index] + A[i] + i - index;
            if (tmp > res) res = tmp;
            if (A[index] + i - index < A[i])
                index = i;
        }
        return res;
    }
};